<?php

/**
 * Task
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu 
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     jack.xie <jack_xie@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

class Calendar {
	private $engineer;
	private $maxtask=0;
	private $dailyData = array();	
	private $year;
	private $month;
	private $day;
	private $monthDays;
	private $dayWeek = array();
	private $data;
	private $nextMonth = array();
	private $lastMonth = array();
	private $week = array(
		0 => 'Sunday',
		1 => 'Monday',
		2 => 'Tuesday',
		3 => 'Wednesday',
		4 => 'Thursday',
		5 => 'Friday',
		6 => 'Saturday',
	);
	
	public function __construct($enginner, $year=null, $month=null, $day=null)
	{
		$this->enginner = $enginner;
		$this->year 	= $year ? $year : date('Y');
		$this->month 	= $month ? $month : date('n');
		$this->day		= $day ? $day : date('d');		
		switch ($this->month) {
			case 2:
				$this->monthDays = ($this->isLeapYear()) ? 29 : 28;	
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				$this->monthDays = 30;
				break;
			default:
				$this->monthDays = 31;
				break;
		}
		$this->setDayWeek();
		$this->setLastMonth();
		$this->setNextMonth();
		$this->getDailyTasks();
	}
	
	private function isLeapYear()
	{
		return  ($this->year % 4 == 0 && $this->year % 100 != 0) 
			 || ($this->year % 400 == 0) ? true : false;
	}		
	
	private function setDayWeek()
	{
		for ($day=1 ; $day <= $this->monthDays; $day++) {
			$this->dayWeek[$day]  = date('w',mktime(0,0,0,$this->month,$day,$this->year));
		}
	}
	
	private function getDailyTasks() {
		//last month last day
		$date = strtotime($this->year."-".$this->month."-01");
		$this->dailyData[0] = $this->getDayTasks(date("Y-m-d", strtotime("-1 day", $date)),1);	
		//loop this month every day		
		for($i=1; $i<=$this->monthDays; $i++) {
			$type = ($i==1) ? 1 : 0;
			$date = strtotime($this->year."-".$this->month."-".$i);
			$this->dailyData[$i] = $this->getDayTasks(date("Y-m-d",$date),$type);
		}
		//next month first day
		$date = strtotime($this->nextMonth['year']."-".$this->nextMonth['month']."-01");
		$this->dailyData[$i] = $this->getDayTasks(date("Y-m-d", $date),1);
		//echo "<pre>";print_r($this->dailyData);echo "</pre>";
	}
	
	private function getTaskKeyInDay($task, $day) {		
		$exist = -1;
		foreach($this->dailyData[$day] as $key => $tt) {
			if ($tt['id'] == $task['id']) {
				$exist = $key;
				break;
			}
		}
		return $exist;
	}
	
	private function getDayTasks($date,$type=0) {
		Zend_Loader :: loadClass('Task');
		$oTask = new Task();
		$params['startDate'] = $date;
		$params['endDate'] = $date;
		$params['engineers'] = $this->enginner;
		$params['orderby'] = 'date_start';
		$tasks = $oTask->getFilteredTasksByRpt($params);	
		//to do	
		if ($type==1) {
			foreach($tasks as $key=>$task) {
				$tasks[$key]['n'] = $key;				
			}
			$tasks['count'] = count($tasks);
			if ($tasks['count'] > $this->maxtask) $this->maxtask = $tasks['count'];
			$data = $tasks;
		} else {		
			$maxkey = 0;
			$data = array();	
			$day = (int)substr($date,8);
			//echo "day=>".($day-1)."<br>";
			foreach($tasks as $task) {				
				$key = $this->getTaskKeyInDay($task,$day-1);
				if ($key == -1) {
					for($i=0; $i<$maxkey; $i++) {
						if (!isset($data[$i])) break;
					}					
					if ($i == $maxkey) $maxkey = ++$i;					
					$task['n'] = ++$this->maxtask;
					$data[$i] = $task;			
					$task['n'] = ++$this->maxtask;
					//$ckey = $i;		
				} else {
					$task['n'] = $this->dailyData[$day-1][$key]['n'];
					$data[$key] = $task;
					if ($key > $maxkey) $maxkey = $key;	
					//$ckey = $key;
				}				
				//echo $task['id']."=> last key $key; => curr key $ckey => n is {$task['n']}<br>";
			}
			$data['count'] = $maxkey+1;
		}
		return $data;
	}
	
	public function getHtmlData()
	{
		$html .= $this->getHead();
		$html .= $this->getMiddle();
		$html .= $this->getTail();
		return $html;
	}
	
	private function setLastMonth()
	{
		if ($this->month == 1)
		{
			$this->lastMonth['month']	= 12;
			$this->lastMonth['year']	= $this->year - 1;
		} 
		else 
		{
			$this->lastMonth['month']	= $this->month -1 ;
			$this->lastMonth['year']	= $this->year;
		}
	}

	private function setNextMonth()
	{
		if ($this->month == 12)
		{
			$this->nextMonth['month'] 	= 1;
			$this->nextMonth['year']  	= $this->year + 1;
		}
		else 
		{
			$this->nextMonth['month']	= $this->month + 1;
			$this->nextMonth['year']	= $this->year;
		}
	}

	private function getHead()
	{
		$html  = "<table class='calendar' cellpadding='0' cellspacing='0'>\r";
		$html .= $this->getTitleHtml();
		$html .= $this->getWeekTitleHtml();
		$html .= "<tr valign='top'>\r";
		return $html;	
	}
	
	private function getMiddle()
	{
		$html  = '';
		$html .= $this->getFirstLineEmptyDayTd();
		$html .= $this->getNotEmptyDayTd();
		$html .= $this->getLastEmptyDayTd();
		return $html;
	}
	
	private function getTail()
	{
		return "</tr></table>";	
	}

	private function getTitleHtml()
	{
		$html = "<tr>
					<th colspan=7>
						<a href='/report/calendar/resourceid/".$this->enginner."/year/{$this->lastMonth['year']}/month/{$this->lastMonth['month']}'>&lt;&lt;&nbsp;&nbsp;</a>
						<b>" . $this->year . "." . $this->month . "</b>
						<a href='/report/calendar/resourceid/".$this->enginner."/year/{$this->nextMonth['year']}/month/{$this->nextMonth['month']}'>&nbsp;&nbsp;&gt;&gt;
						</a>
					</th>
				  </tr>
				 ";	
		return $html;
	}

	private function getWeekTitleHtml()
	{
		$html = "<tr>\r";
		foreach ($this->week as $key => $value) {
			$html .= "<td class='weekname'>$value</td>\r";	
		}
		return $html."</tr>\r";
	}

	private function getFirstLineEmptyDayTd()
	{
		$html = "";
		for ($week = 0; $week < $this->dayWeek[1]; $week++) {
			$html .= $this->getEmptyDateTd();	
		}
		return $html;
	}
	
	private function getNotEmptyDayTd()
	{
		$html = '';
		for ($day = 1; $day <= $this->monthDays; $day++) {
			$html .= $this->getDateTd($day);
			if ($this->dayWeek[$day] == 6) {
				$html .= "</tr>\r<tr valign='top'>\r";	
			}
		}
		return $html;
	}

	private function getLastEmptyDayTd()
	{
		$html = '';;
		for ($week = $this->dayWeek[$this->monthDays] + 1; $week < 7; $week++) {
			$html .= $this->getEmptyDateTd();	
		}
		return $html;
	}
	
	private function getEmptyDateTd()
	{
		return "<td class='tddate empty'>&nbsp;</td>\r";
	}
	
	private function getDateTd($day)
	{
		$class = "";
		if ($this->dayWeek[$day] == 6 || $this->dayWeek[$day] ==0) {
			$class = "weekend";
		}
		$html = "<td class='tddate $class'><div class='day'>" . $day . "</div>";
		$html .= $this->getDataTd($day);
		$html .= "</td>\r";
		return $html;	
	}
	
	private function getDataTd($day)
	{
		$html = '';
		/*
		if ($this->dayWeek[$day] == 0 || $this->dayWeek[$day] == 6) {
			return '&nbsp;';
		}*/
		if (!empty($this->dailyData[$day])) {
			for ($i=0; $i<$this->dailyData[$day]['count']; $i++) {									
				if (empty($this->dailyData[$day][$i])) {	
					$html .= "<div class='task empty'>&nbsp;</div>";			
					continue;
				} else {
					$task =  $this->dailyData[$day][$i];	
				}
				
				$start = "";
				$end = "";
				$class = "task li".($task['n']%20);
				$text = "&nbsp;";
				$content = "<a href='javascript:viewtask({$task['id']});void(0);'>[{$task['id']}]</a> &nbsp;"
						 . substr($task['title'],0,15);
				if ($this->getTaskKeyInDay($task, $day-1) == -1) {
					$class .= " taskstart";
					$text = $content;					
				}
				if ($this->getTaskKeyInDay($task, $day+1) == -1) {
					$class .= " taskend";
					$end = "<div class='de'>&nbsp;</div>";	
				}
				if ($this->dayWeek[$day] == 1) {
					$class .= " weekstart";
					$start = "&lt;";
					$text = $content;		
				}
				if ($this->dayWeek[$day] == 5) {
					$class .= " weekend";
					if (strpos($class,"taskend")===false) {
						$end = "<span class='de'>&gt;</span>";
					}
				}
				if ($this->dayWeek[$day] == 0 || $this->dayWeek[$day] == 6) {
					$class .= "";
					$start = "";
					$text = "&nbsp;";
				}
				if ($day == 1 && $this->dayWeek[$day] != 0) {
					$class .= " monthstart";
					$start = "&lt;";
					$text = $content;		
				}
				if ($day == $this->monthDays) {
					$class .= " monthend";
					if (strpos($class,"taskend")===false) {
						$end = "<span class='de'>&gt;</span>";
					}
				}
				$html .= "<div class='$class' title='{$task['title']}'><span class='dm'>$start $text</span>$end</div>";
			}
		}
		return $html;
	}
}
?>